const mysql = require('mysql');

let pool = mysql.createPool({
  host:'localhost',
  user:'root',
  password:'1111',
  database:'elm',
});

let query = function( sql, values ) {
  return new Promise(( resolve, reject ) => {
    pool.getConnection(function(err, connection) {
      if (err) {
        resolve( err )
      } else {
        connection.query(sql, values, ( err, data) => {
          if ( err ) {
            reject( err )
          } else {
            resolve( data )
          }
          connection.release()
        })
      }
    })
  });
}

let status = {
  userId: 0, //当前登录用户ID
  sellerId: 0, //当前访问的商家ID
}

/***********用户API*****************/


//用户登录校验
let userLogin = function ( username ) {
	let sql = `SELECT * FROM user WHERE username = '${username}'`;
	return query(sql);
}

//用户ID全局化
let initUserId = function ( userId ) {
    status.userId = userId;
}

//获取用户基本信息
let getUserInfo = function(  ) {
  let sql = `SELECT * FROM userinfos WHERE userId = ${status.userId}`;
  return query(sql);
}

//获取用户订单信息
let getUserOrders = function( type ){
  let sql;
  if(type === '0'){
    sql = `SELECT * FROM orders WHERE userId = ${status.userId} ORDER BY orderTime DESC`;
  }
  else{
    sql = `SELECT * FROM orders WHERE orderType = ${type} AND userId = ${status.userId} ORDER BY orderTime DESC`;
  }
  return query(sql);
}

//扣除用户余额
let userAccountSub = function( account ){
  let sql;
  sql = `UPDATE userinfos SET account = ${account} WHERE userId = ${status.userId}`;
  return query(sql);
}

//用户确认收货
let userConfirmOrder = function( orderId ){
  let sql;
  sql = `UPDATE orders SET orderType = 2 WHERE orderId = ${orderId}`;
  return query(sql);
}

//用户完成评价
let userRatedOrder = function( orderId ){
  let sql;
  sql = `UPDATE orders SET orderType = 3 WHERE orderId = ${orderId}`;
  return query(sql);
}

//用户下单
let userSubmitOrder = function( order ){
  let sql;
  sql = `INSERT INTO orders(userId,sellerId,orderTime,price,orderType) VALUES(${status.userId},${status.sellerId},${order.orderTime},${order.price},1);`;
  return query(sql);
}

//用户评价
let userSubmitRating = function( rating ){
  let sql;
  sql = `INSERT INTO sellerratings (userId,sellerId,orderId,rateTime,deliveryTime,score,rateType,TEXT) VALUES(${status.userId},${status.sellerId},${rating.orderId},${rating.rateTime},${rating.deliveryTime},${rating.score},${rating.rateType},'${rating.text}');`;
  return query(sql);
}


/***********商品API*****************/


//获取全部商品类型
let getGoodTypes = function ( ) {
  let sql = `SELECT * FROM foodtype`;
  return query(sql);
};

//获取某一类型的全部商品
let getGoodsByType = function( goodType ) {
  let sql = `SELECT * FROM food WHERE fTypeId = ${goodType}`;
  return query(sql);
};

//获取指定Id的某一商品
let getGoodByGoodId = function( goodId ) {
	let sql = `SELECT * FROM food WHERE foodId = ${goodId}`;
  return query(sql);
};


/***********商家API*****************/

//商家ID全局化
let initSellerId = function ( sellerId ) {
    status.sellerId = sellerId;
}

//获取商家基本信息
let getSellerInfo = function( ){
  let sql = `SELECT * FROM seller`;
  return query(sql);
};

//获取商家优惠信息
let getSellerSupports = function( ){
  let sql = `SELECT * FROM sellersupports WHERE sellerId = ${status.sellerId}`;
  return query(sql);
};

//获取商家实景信息
let getSellerPics = function( ){
  let sql = `SELECT * FROM sellerpics WHERE sellerId = ${status.sellerId}`;
  return query(sql);
};

//获取商家介绍信息
let getSellerInfos = function( ){
  let sql = `SELECT * FROM sellerinfos WHERE sellerId = ${status.sellerId}`;
  return query(sql);
};

//获取商家评价信息
let getSellerRatings = function( type ){
  let sql;
  if(type === '0'){
    sql = `SELECT * FROM sellerratings WHERE sellerId = ${status.sellerId} ORDER BY rateTime DESC`;
  }
  else{
    sql = `SELECT * FROM sellerratings WHERE rateType = ${type} AND sellerId = ${status.sellerId} ORDER BY rateTime DESC`;
  }
  return query(sql);
};


module.exports = {
	getGoodTypes,
	getGoodsByType,
	getGoodByGoodId,
  initUserId,
	userLogin,
  userConfirmOrder,
  userSubmitOrder,
  userAccountSub,
  userRatedOrder,
  userSubmitRating,
  getUserInfo,
  getUserOrders,
  initSellerId,
  getSellerInfo,
  getSellerSupports,
  getSellerPics,
  getSellerInfos,
  getSellerRatings,
};


